package in.muton.edushare.poll.panels;

import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.poll.components.EditLink;
import in.muton.edushare.poll.components.PreviewLink;
import in.muton.edushare.poll.components.ResultLink;
import in.muton.edushare.poll.components.VoteLink;
import in.muton.edushare.url.EduShareURLPanel;
import in.muton.edushare.wicket.models.CayenneModel;

import java.text.SimpleDateFormat;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.edushare.common.vo.Poll;
import com.edushare.common.vo.Users;

public class PollListItem extends EdusharePanel {
	private static final long serialVersionUID = -1559345837149055307L;

	public PollListItem(String id, IModel<Poll> model) {
		super(id, model);
		final Poll p = model.getObject();
		add(new Label("pollName", p.getPollName()));

		add(new Label("startDate", new Model<String>() {
			@Override
			public String getObject() {
				if (p.getPollStartDate() == null) {
					return "Not started";
				} else {
					return new SimpleDateFormat("MMM d, yyyy").format(p
							.getPollStartDate());
				}
			}

			private static final long serialVersionUID = -8132824644427377068L;
		}));
		add(new Label("audience", p.getPollAudi(getORMInterface())));
		add(new Label("endDate", new SimpleDateFormat("MMM d, yyyy").format(p
				.getPollEndDate())));

		add(new Label("privacy", new Model<String>() {
			private static final long serialVersionUID = -2291001778961399762L;

			@Override
			public String getObject() {
				if (p.getPollResultsPublic() == Poll.POLL_RESULT_OPEN)
					return "Participants can view results after voting.";
				return "Participants do not have access to poll results.";
			}
		}));

		add(new Label("anonymity", new Model<String>() {
			private static final long serialVersionUID = -1249393214706559203L;

			@Override
			public String getObject() {
				if (p.getPollAnonymous() == Poll.ANONYMOUS_POLL)
					return "Anonymous poll. Participants' details will not be stored when voting.";
				return "Participants' details will be stored when voting.";
			}
		}));

		add(new Label("status", new Model<String>() {
			@Override
			public String getObject() {
				switch (p.getStatus()) {
				case (Poll.POLL_STATUS_DRAFT):
					return "Draft";
				case (Poll.POLL_STATUS_POLLING):
					return "Open";
				case (Poll.POLL_STATUS_CLOSED):
					return "Closed";
				}
				return "";
			}

			private static final long serialVersionUID = -8132824644427377068L;
		}));
		add(new EduShareURLPanel("creator", new CayenneModel<Users>(p
				.getCreator())));
		add(new VoteLink("vote", model));
		add(new EditLink("edit", model));
		add(new ResultLink("result", model));
		add(new PreviewLink("preview", model));
	}
}
